package com.mashibing.service.impl;

import com.mashibing.internalcommon.constant.CommonStatusEnum;
import com.mashibing.internalcommon.dto.PassengerUser;
import com.mashibing.internalcommon.dto.ResponseResult;
import com.mashibing.mapper.UserMapper;
import com.mashibing.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public ResponseResult<Void> loginOrRegister(String passengerPhone) {

        // 根据号码查询用户信息
        HashMap<String, Object> map = new HashMap<>();
        map.put("passenger_phone",passengerPhone);
        List<PassengerUser> passengerUsers = userMapper.selectByMap(map);

        // 判断
        if (passengerUsers.isEmpty()) {
            // 没有就添加
            PassengerUser passengerUser = new PassengerUser();
            passengerUser.setPassengerPhone(passengerPhone);
            passengerUser.setPassengerName("张三");
            passengerUser.setPassengerGender((byte) 0);

            LocalDateTime now = LocalDateTime.now();
            passengerUser.setGmtCreate(now);
            passengerUser.setGmtModified(now);

            userMapper.insert(passengerUser);
        }

        return ResponseResult.success();
    }

    @Override
    public ResponseResult<PassengerUser> getUserByPassengerPhone(String phone) {

        HashMap<String, Object> map = new HashMap<>();
        map.put("passenger_phone",phone);
        List<PassengerUser> passengerUsers = userMapper.selectByMap(map);
        if (passengerUsers.isEmpty()) {
            return ResponseResult.fail(CommonStatusEnum.USER_NOT_EXISTS.getCode(),CommonStatusEnum.USER_NOT_EXISTS.getMessage(),null);
        }

        PassengerUser passengerUser = passengerUsers.get(0);
        log.info("电话为" + phone + "的用户信息为：" + passengerUser);
        return ResponseResult.success(passengerUser);
    }


























}
